﻿#include "volumebutton.h"

#include <QtWidgets>
#include <QtWinExtras>

VolumeButton::VolumeButton(QWidget *parent) :
    QToolButton(parent)
{

    this->setToolTip("设置音量");
    this->setStyleSheet("QToolButton{border: 0px; border-style: none;height: 22px; width: 22px; border-radius:5px;} QToolButton:hover{background-color: rgba(255, 255, 255, 0.2);} QToolButton::menu-indicator{image:none;}");

    setIcon(style()->standardIcon(QStyle::SP_MediaVolume));
    setPopupMode(QToolButton::InstantPopup);


    QWidget *popup = new QWidget(this);

    butMute = new QToolButton();
    butMute->setToolTip("切换静音");
    butMute->setIcon(style()->standardIcon(QStyle::SP_MediaVolume));
    butMute->setCheckable(true);
    butMute->setStyleSheet("QToolButton{height: 20px; width: 20px; border-radius:0px;} QToolButton:hover{border: 1px solid #2993D1;} QToolButton:checked{background: none;}");
    connect(butMute, SIGNAL(clicked(bool)), this, SLOT(onMuteChanged(bool)));

    slider = new QSlider(Qt::Horizontal, popup);
    slider->setRange(0, 100);
    slider->setValue(100);
    connect(slider, &QAbstractSlider::valueChanged, this, &VolumeButton::onVolumeChanged);

    label = new QLabel(popup);
    label->setAlignment(Qt::AlignCenter);
    label->setNum(100);
    label->setMinimumWidth(label->sizeHint().width());

    connect(slider, &QAbstractSlider::valueChanged, label, QOverload<int>::of(&QLabel::setNum));

    QBoxLayout *popupLayout = new QHBoxLayout(popup);
    popupLayout->setMargin(2);
    popupLayout->addWidget(butMute);
    popupLayout->addWidget(slider);
    popupLayout->addWidget(label);

    QWidgetAction *action = new QWidgetAction(this);
    action->setDefaultWidget(popup);

    menu = new QMenu(this);
    menu->addAction(action);
    setMenu(menu);

    stylize();
}

void VolumeButton::increaseVolume()
{
    slider->triggerAction(QSlider::SliderPageStepAdd);
}

void VolumeButton::descreaseVolume()
{
    slider->triggerAction(QSlider::SliderPageStepSub);
}

int VolumeButton::volume()
{
    return slider->value();
}

void VolumeButton::setVolume(int volume)
{
    slider->setValue(volume);
}

void VolumeButton::onVolumeChanged(int value)
{
    this->onMuteChanged(value == 0);
    emit volumeChanged(value);
}

void VolumeButton::onMuteChanged(bool mute)
{
    this->setIcon(style()->standardIcon(mute ? QStyle::SP_MediaVolumeMuted : QStyle::SP_MediaVolume));
    butMute->setIcon(style()->standardIcon(mute ? QStyle::SP_MediaVolumeMuted : QStyle::SP_MediaVolume));
    emit muteChanged(mute);
}

//! [0]
void VolumeButton::stylize()
{
    if (QOperatingSystemVersion::current() < QOperatingSystemVersion::Windows8) {
        // Set styling options relevant only to Windows 7.
        if (QtWin::isCompositionEnabled()) {
            QtWin::enableBlurBehindWindow(menu);
            QString css("QMenu { border: 1px solid %1; border-radius: 2px; background: transparent; }");
            menu->setStyleSheet(css.arg(QtWin::realColorizationColor().name()));
        } else {
            QtWin::disableBlurBehindWindow(menu);
            QString css("QMenu { border: 1px solid black; background: %1; }");
            menu->setStyleSheet(css.arg(QtWin::realColorizationColor().name()));
        }
    }
}
//! [0]
